package com.atguigu.es;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;

import java.io.IOException;

public class WriteSingleDoc {

    public static void main(String[] args) throws IOException {
        //一.创建和es数据库的连接,获取一个客户端

            //1. new出这个工厂
        JestClientFactory factory = new JestClientFactory();
            //2. 看源码这个工厂有连接es地址的默认属性,得主动为属性赋值,否则连不上
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        factory.setHttpClientConfig(httpClientConfig);
            //3.通过工厂得到jestclient
        JestClient client = factory.getObject();

        //二.中间操作
            //1.不使用beans封装json请求体
        String source = "{\"empid\":1001,\"age\":20,\"balance\":2000,\"name\":\"李三1\",\"gender\":\"男\",\"hobby\":\"吃饭睡觉\"}";
            //2.建造者建造写入对象
        Index write = new Index.Builder(source).index("test").type("emps").id("1000000").build();
            //3.执行写入
        DocumentResult result = client.execute(write);
            //4.打印得到的响应码
        System.out.println(result.getResponseCode());

        //关闭客户端
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
